package com.cooper.core;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class JsonResult extends JSONObject {
	
	private static final long serialVersionUID = 155493030636726896L;
	
	public static final String SUCCESS = "success";
	public static final String MESSAGE = "message";
	
	public JsonResult(){
		this.put(SUCCESS, false);
	}
	
	public void setSuccess(boolean success) {
		this.put(SUCCESS, success);
		if(true == success && StringUtils.isBlank(this.getMessage())){
			this.setMessage("Success!");
		}
	}
	
	public void setMessage(String message) {
		this.put(MESSAGE, message);
	}
	
	public boolean isSuccess(){
		return this.getBooleanValue(SUCCESS);
	}
	
	public String getMessage(){
		return this.getString(MESSAGE);
	}
	
	public void setMessage(String message, Object... objects){
		String text = message;
		for(int index=0;index < objects.length; index++){
			text = text.replace("{"+index+"}", String.valueOf(objects[index]));
		}
		this.put(MESSAGE, text);
	}
	
}
